package com.zcsy.manage.web.news;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.news.YellowPage;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.common.CommonService;
import com.zcsy.manage.service.news.INoticeInfoService;
import com.zcsy.manage.service.news.IYellowPageService;

/**
 * @Title: YellowPageController.java
 * @Package： com.zcsy.manage.web.news 
 * @Description: 便民黄页控制层管理类
 * @author liuying 1075817474@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年8月2日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/yellowPage")
public class YellowPageController {

	@Autowired
	private IYellowPageService yellowPageService;

	@Autowired
	private INoticeInfoService noticeInfoService;

	private static final int PAGE_SIZE = 12; //每页大小
	
	/**
	 * @Description 便民黄页列表展示
	 * @param pNo 当前页
	 * @param pageName 黄页名
	 * @return 跳转画面
	 * @author liuying 1075817474@qq.com
	 * @date 2016年8月2日
	 * @see 
	 */
	@RequestMapping(value = "yellowPageList", method = RequestMethod.GET)
	public String yellowPageList(Model model, Integer pNo, String pageName, HttpServletRequest req) {

	    String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
		User user = CommonService.getSessionUser();
		
		if (pNo == null) {
			pNo = Constants.PAGE_NO;
		}

		// 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
		PageHelper.startPage(pNo, PAGE_SIZE);

		YellowPage yellowPage = new YellowPage();

        yellowPage.setCellId(cellId);
        yellowPage.setPageName(pageName);
		
		List<YellowPage> yellowPages = yellowPageService.selectByKeyword(yellowPage);
		PageInfo<YellowPage> info = new PageInfo<YellowPage>(yellowPages);

		//未读条数查询
        int unRead = noticeInfoService.selectUnreadCount(user.getId(), cellId);
		
		model.addAttribute("yellowPages", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("unRead", unRead);
		model.addAttribute("pageName", pageName);
		
		return "news/yellowpage/yellowPageList";
	}
}

